/*
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2022-09-06 09:03:52
 * @LastEditors: voanit
 * @LastEditTime: 2022-09-11 17:59:08
 */
import { defineStore } from "pinia"
import request from '@/utils/request'
import { ApiRes, CategoryItem, TopCategory, SubCategory } from '@/types/data'
import { topCategory } from '@/store/constants'

const defaultCategory = topCategory.map(item => {
  return {
    name: item,

  }
})
//  [{name:''},{}]
const useCategoryStore = defineStore('category', {
  state: () => {
    return {
      // list: [] as CategoryItem[],//类型断言
      list: defaultCategory as CategoryItem[],//类型断言
      topCategory: {} as TopCategory,
      subCategory: {} as SubCategory,
    }
  },
  actions: {
    async getAllCategory() {
      const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
      console.log(res)
      this.list = res.data.result
    },
    async findTopCategory(id: string) {
      const res = await request.get<ApiRes<TopCategory>>('/category', {
        params: {
          id,
        },
      })
      this.topCategory = res.data.result
    },
    async getSubFilter(id: string) {
      const res = await request.get<ApiRes<SubCategory>>(
        '/category/sub/filter',
        {
          params: {
            id,
          },
        }
      )
      this.subCategory = res.data.result
    },
  },
  getters: {}
})

export default useCategoryStore